package com.base.cn.platform.os.controller.manage.loreset;

import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.manage.loreset.SysLoresetPoint;
import com.base.cn.platform.os.service.manage.loreset.SysLoresetPointBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 知识体系目录Controller
 *
 * @author s.li
 * @create 2018-03-22 17:28
 */
@RestController
@RequestMapping("/manage")
public class SysLoresetPointController extends BaseController {

    @Autowired
    private SysLoresetPointBiz sysLoresetPointBiz;

    /**
     * 删除知识点数据
     *
     * @param ids 知识点ID串
     * @return Map&lt;String,Object&gt;
     */
    @RequestMapping("/deleteLoresetPoint")
    public Map<String, Object> deleteLoresetPoint(@RequestParam("ids") String ids) {
        sysLoresetPointBiz.deleteLoresetPoint(ids);
        return ResultUtil.SUCCESS("知识点删除成功");
    }

    /**
     * 修改知识点的父级归属
     *
     * @param id       知识点ID
     * @param parentId 知识点的父级节点
     * @return Map&lt;String,Object&gt;
     */
    @RequestMapping("/updateLoresetPointParentId")
    public Map<String, Object> updateLoresetPointParentId(@RequestParam("id") BigDecimal id, @RequestParam("parentId") BigDecimal parentId) {
        sysLoresetPointBiz.updateLoresetPointParentId(id, parentId);
        return ResultUtil.SUCCESS("修改知识的父级归属成功");
    }

    /**
     * 保存知识点数据
     *
     * @param loresetPoint 知识点数据对象
     * @return Map&lt;String,Object&gt;
     */
    @RequestMapping("/saveLoresetPoint")
    public Map<String, Object> saveLoresetPoint(@RequestBody SysLoresetPoint loresetPoint) {
        if (StringUtils.isEmpty(loresetPoint.getPointName())) {
            return ResultUtil.ERROR("知识点名称不能为空");
        }
        if (loresetPoint.getPointName().trim().length() > 30) {
            return ResultUtil.ERROR("知识点名称限制在30个字以内");
        }
        if (StringUtils.isNotEmpty(loresetPoint.getTeachingNamePage()) && loresetPoint.getTeachingNamePage().trim().length() > 255) {
            return ResultUtil.ERROR("对应教材名称与页码说明内容限制在255个字以内");
        }
        if (StringUtils.isNotEmpty(loresetPoint.getDepict()) && loresetPoint.getDepict().trim().length() > 500) {
            return ResultUtil.ERROR("知识点说明内容限制在500个字以内");
        }
        //占分值字段目前没有用到暂时注释,勿删
        /*if (StringUtils.isEmpty(loresetPoint.getFraction())) {
            return ResultUtil.ERROR("请占分值说明内容");
        }
        if (loresetPoint.getFraction().trim().length() > 255) {
            return ResultUtil.ERROR("占分值说明内容限制255个字以内");
        }*/
        loresetPoint = sysLoresetPointBiz.saveLoresetPoint(loresetPoint);
        return ResultUtil.SUCCESS("知识点数据保存成功", loresetPoint);
    }

    /**
     * 通过知识体系ID，获取知识点数据点列表
     *
     * @param loresetIds 知识体系ID
     * @return List&lt;SysLoresetPoint&gt;知识点数据列表
     */
    @RequestMapping("/findLoresetPointListByLoresetIds")
    public List<SysLoresetPoint> findLoresetPointListByLoresetIds(@RequestParam("loresetIds") String loresetIds) {
        return sysLoresetPointBiz.findLoresetPointListByLoresetIds(loresetIds);
    }

    /**
     * 通过知识体系ID串查询知识点数据列表
     *
     * @param loresetIds 知识点ID串
     * @return 分层且分组好的知识点数据Map，以知识体系Key，value是知识体系下的知识点列表且这个知识列表是分层好的
     */
    @RequestMapping("/findLayeredLoresetPoint")
    public Map<BigDecimal, List<SysLoresetPoint>> findLayeredLoresetPoint(@RequestParam("loresetIds") String loresetIds) {
        return sysLoresetPointBiz.findLayeredLoresetPoint(loresetIds);
    }

    /**
     * 通过ID获取知识点数据
     *
     * @param id 知识ID
     * @return 知识点对象
     */
    @RequestMapping("/findLoresetPointById")
    public SysLoresetPoint findLoresetPointById(@RequestParam("id") BigDecimal id) {
        return sysLoresetPointBiz.findById(id);
    }

    /**
     * 通过ID获取知识点数据
     *
     * @param ids     知识点id
     * @param layered 分层，即二级知识点放入一级知识点
     * @return 考点
     */
    @RequestMapping("/findLoresetPointByIdsToMap")
    public Map<BigDecimal, SysLoresetPoint> findLoresetPointByIdsToMap(@RequestParam("ids") String ids,
                                                                  @RequestParam("layered") boolean layered) {
        return sysLoresetPointBiz.findLoresetPointByIdsToMap(ids, layered);
    }

    /**
     * 刷新考点试题量
     * @param countMap 更新参数
     * @param status 试题状态
     */
    @RequestMapping("/refreshLoresetPoint")
    public void refreshLoresetPoint(@RequestBody Map<String, Integer> countMap,
                                    @RequestParam("status") int status) {
        sysLoresetPointBiz.refreshLoresetPoint(countMap,status);
    }

}
